package com.hmdp.runner;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hmdp.entity.ShopType;
import com.hmdp.mapper.ShopTypeMapper;
import com.hmdp.service.IShopTypeService;
import com.hmdp.service.impl.ShopTypeServiceImpl;
import com.hmdp.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

import static com.hmdp.utils.SystemConstants.SHOP_TYPE_LIST;

@Component
public class ShopListRunner implements CommandLineRunner {
    @Autowired
    private RedisCache redisCache;
    @Autowired
    ShopTypeMapper shopTypeMapper;

    @Override
    public void run(String... args) throws Exception {
        if (Objects.nonNull(redisCache.getCacheList(SHOP_TYPE_LIST))){
            redisCache.deleteObject(SHOP_TYPE_LIST);
            writeCache();
            return;
        }
        writeCache();
    }

    public void writeCache(){
        LambdaQueryWrapper<ShopType> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(ShopType::getSort);
        //于数据库查询shopList
        List<ShopType> shopTypes = shopTypeMapper.selectList(queryWrapper);
        //将shopList存入redis中
        redisCache.setCacheList(SHOP_TYPE_LIST,shopTypes);
        System.out.println("------已将shopTypeList存入缓存------");
    }
}











